<template>
  <el-form ref="formRef" :model="form" :rules="rules" label-width="160px" class="withdraw-form">
    <el-form-item label="提现时间" required>
      <el-time-picker
        v-model="timeRange"
        is-range
        range-separator="至"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        format="HH:mm"
        value-format="HH:mm"
      />
    </el-form-item>

    <el-form-item label="最小提现金额(元)" prop="minDrawAmount">
      <el-input v-model.number="form.minDrawAmount" type="number" />
    </el-form-item>

    <el-form-item label="是否免审" prop="auditStatus">
      <el-radio-group v-model="form.auditStatus">
        <el-radio :label="true" :value="1">免审</el-radio>
        <el-radio :label="false" :value="0">需要审核</el-radio>
      </el-radio-group>
    </el-form-item>

    <el-form-item label="分润免审金额(元)" prop="unauditAmount">
      <el-input-number v-model="form.unauditAmount" :min="0" :step="1" />
    </el-form-item>

    <el-form-item label="提现税点(%)" prop="taxRate">
      <el-input v-model.number="form.taxRate" type="number" />
    </el-form-item>

    <el-form-item label="提现单笔(元)" prop="debitAmount">
      <el-input v-model.number="form.debitAmount" type="number" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
// import { onMounted, reactive, ref, toRefs } from 'vue'
import { getPaySetting, editPaySetting } from '@/api/finance/paysetting'

const { proxy } = getCurrentInstance()
const timeRange = ref([])
const data = reactive({
  form: {},
  rules: {
    minDrawAmount: [{ required: true, message: '请输入最小提现金额', trigger: 'blur' }],
    auditStatus: [{ required: true, message: '请选择是否免审', trigger: 'change' }],
    unauditAmount: [{ required: true, message: '请输入分润免审金额', trigger: 'blur' }],
    taxRate: [{ required: true, message: '请输入提现税点', trigger: 'blur' }],
    debitAmount: [{ required: true, message: '请输入提现单笔', trigger: 'blur' }]
  }
})

const { form, rules } = toRefs(data)

// 提交
function submit() {
  proxy.$refs['formRef'].validate((valid) => {
    if (!valid) return
    if (valid) {
      if(timeRange.value === null){
        proxy.$message.error('请选择提现时间')
        return false
      }
      const getTimeRange = proxy.addDateRange(timeRange.value)
      form.value.startTime = getTimeRange[0]
      form.value.endTime = getTimeRange[1]
      editPaySetting({ ...form.value }).then((res) => {
        proxy.$message.success('保存成功')
      })
    } else {
      return false
    }
  })
}

onMounted(() => {
  fetchData()
})

// 获取代付信息
function fetchData() {
  getPaySetting(1).then((res) => {
    form.value = res.data
    timeRange.value = [res.data.startTime, res.data.endTime]
  })
}
</script>

<style scoped>
.withdraw-form {
  max-width: 500px;
  padding: 20px;
}
</style>
